package com.decoration.basic.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.decoration.basic.entity.*;
import com.decoration.basic.findCondition.DecPhaseFindCondition;
import com.decoration.basic.mapper.DecCtctionFlowMapper;
import com.decoration.basic.mapper.DecCtctionPhaseMapper;
import com.decoration.basic.findCondition.DecFlowFindCondition;
import com.decoration.basic.po.DecPhaseVo;
import com.decoration.basic.service.DecFlowService;
import com.decoration.basic.service.DecUserService;
import com.decoration.basic.util.SnowFlake;
import com.decoration.basic.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DecFlowServiceImpl implements DecFlowService {

    @Resource
    DecCtctionFlowMapper decCtctionFlowMapper;
    @Resource
    DecCtctionPhaseMapper decCtctionPhaseMapper;
    @Resource
    private DecUserService decUserService;

    @Override
    public List<DecFlowFindCondition> findAll(DecFlowFindCondition findCondition) {
        List<DecFlowFindCondition> result = decCtctionFlowMapper.findAll(findCondition);
        return result;
    }

    @Override
    public List<DecPhaseBase> findTempleList() {
        List<DecPhaseBase> result = decCtctionFlowMapper.findTempleList();
        return result;
    }

    @Override
    public List<DecPhaseBase> findPhaseTempleList() {
        List<DecPhaseBase> result = decCtctionFlowMapper.phaselist();
        return result;
    }

    @Override
    public List<DecPhaseBase> findMasterTempleList() {
        return decCtctionFlowMapper.masterTempleList();
    }

    @Override
    public Boolean addCtcFlow(DecFlowFindCondition findCondition) {
        int i = 0;
        if (findCondition.getPhaselist() != null && findCondition.getPhaselist().size() > 0) {
            //1.添加阶段信息
            for (DecPhaseVo phaseVo : findCondition.getPhaselist()) {
                if (phaseVo.getPhaseId() == null || phaseVo.getPhaseId().length() < 1) {
                    continue;
                }
                //获取旧的阶段信息
                DecPhaseInf oldPhase = decCtctionFlowMapper.searchPhaseInf(phaseVo.getPhaseId());

                if (phaseVo.getStartTime() != null && !StringUtils.isEmpty(phaseVo.getStartTime())) {
                    oldPhase.setStartTime(phaseVo.getStartTime());
                }
                if (phaseVo.getEndTime() != null && !StringUtils.isEmpty(phaseVo.getEndTime())) {
                    oldPhase.setEndTime(phaseVo.getEndTime());
                }
                /*if (phaseVo.getVaildFlag() != null ) {
                    oldPhase.setVaildFlag(phaseVo.getVaildFlag());
                }*/

                //更新阶段信息
                decCtctionFlowMapper.updatePhaseInf(oldPhase);

                //删除原阶段师傅信息
                decCtctionPhaseMapper.deleteMasterUserlist(phaseVo.getPhaseId());
                //2.添加阶段师傅信息
                if (phaseVo.getUserlist() != null && phaseVo.getUserlist().size() > 0) {
                    //新增阶段师傅信息
                    for (PhaseUsers user : phaseVo.getUserlist()) {
                        PhaseUsers masteruser = new PhaseUsers();
                        masteruser.setPhaseId(phaseVo.getPhaseId());
                        masteruser.setCtcId(phaseVo.getCtcId());
                        masteruser.setCtcUserType(user.getCtcUserType());
                        //获取师傅名称
                        DecorationUser user1 = decUserService.findById(user.getPerId());
                        masteruser.setUserName(user1.getPerName());

                        masteruser.setPerId(user.getPerId());
                        decCtctionFlowMapper.insertMasterlist(masteruser);
                    }
                }
                i++;
            }
        }
        System.out.println("更新阶段条数"+i);
        return i == 0;
    }

    @Override
    public List<DecPhaseBase> findMasterTypeList() {
        return decCtctionFlowMapper.findMasterTypeList();
    }

    @Override
    public List<DecFlowFindCondition> findAllTemple(DecFlowFindCondition findCondition) {
        return decCtctionFlowMapper.findAllTemple(findCondition);
    }

    @Override
    public DecFlowFindCondition findDecFlowbyId(DecFlowFindCondition findCondition) {
        return decCtctionFlowMapper.findFlowInfById(findCondition);
    }

    @Override
    public int addPhaseUser(DecFlowFindCondition findCondition) {
        int count = decCtctionFlowMapper.getDecFlowInfLength(findCondition.getFlowId());
        if (count != 1) {
            return 0;
        }
        int num = 0;
        for (DecPhaseVo decPhaseBase : findCondition.getPhaselist()) {
            if (decPhaseBase.getPhaseRank() == null) {
                continue;
            }
            //校验阶段名称属于参数表，不属于不插入
            if (decPhaseBase.getPhaseRank() == null) {
                int decPhaseNamecount = decCtctionPhaseMapper.verifyPhaseName(decPhaseBase.getPhaseName());
                if (decPhaseNamecount != 1) {
                    continue;
                }
            }
            //校验阶段状态，只可修改未申请的阶段师傅明细
            if (decPhaseBase.getVaildFlag() != 0 || decPhaseBase.getVaildFlag() != 1) {
                continue;
            }
            //获取阶段内容
            DecPhaseVo lphase = decCtctionPhaseMapper.getPhaseInf(decPhaseBase.getPhaseId());
            if (lphase == null || lphase.getPhaseId() == null) {
                continue;
            }
            /*Integer counts = decCtctionPhaseMapper.countPhaseInf(decPhaseBase.getPhaseId());
            if (counts == 0) continue;*/

            //删除阶段师傅信息
            decCtctionPhaseMapper.deleteMasterUserlist(decPhaseBase.getPhaseId());
            //插入阶段师傅信息
            for (PhaseUsers masterUser : decPhaseBase.getUserlist()) {
                masterUser.setCtcId(lphase.getCtcId());
                masterUser.setPhaseId(lphase.getPhaseId());
                decCtctionPhaseMapper.addPhaseUser(masterUser);
            }
            num++;
        }
        return 1;
    }


    //插入流程模板
    @Override
    public int insert(DecFlowFindCondition findCondition) {

        String flowid = String.valueOf(SnowFlake.getInstance().nextId());
        DecFlowInf decFlowInf = new DecFlowInf();
        decFlowInf.setFlowId(flowid);
        decFlowInf.setFlowName(findCondition.getFlowName());
        decFlowInf.setRemark(findCondition.getRemark());
        decFlowInf.setFlowFlag(0);
        decFlowInf.setPerId(findCondition.getPerId());
        decFlowInf.setCtcId("temple");

        int res = decCtctionFlowMapper.add(decFlowInf);

        int szphaseRank = 0;
        for (DecPhaseVo decPhaseBase : findCondition.getPhaselist()) {
            /*if (decPhaseBase.getPhaseRank() == null) {
                continue;
            }*/
            //校验阶段名称属于参数表，不属于不插入
            if (decPhaseBase.getPhaseRank() == null) {
                int decPhaseNamecount = decCtctionPhaseMapper.verifyPhaseName(decPhaseBase.getPhaseName());
                if (decPhaseNamecount != 1) {
                    continue;
                }
            }
            DecPhaseInf decPhaseInf = new DecPhaseInf();
            decPhaseInf.setPhaseId(String.valueOf(SnowFlake.getInstance().nextId()));
            decPhaseInf.setPhaseName(decPhaseBase.getPhaseName());
            decPhaseInf.setPhaseRank(szphaseRank);
            decPhaseInf.setFlowId(flowid);
            if (StringUtil.isEmpty(decPhaseBase.getPhaseRank()) || decPhaseBase.getPhaseRank() == 0) {
                decPhaseInf.setVaildFlag(1);
            } else {
                decPhaseInf.setVaildFlag(0);
            }
            decPhaseInf.setCtcId("temple");

            int rs = decCtctionPhaseMapper.addPhase(decPhaseInf);

            szphaseRank++;
        }

        return res;
    }

    @Override
    public int updateById(DecFlowFindCondition findCondition) {

        DecFlowInf decFlowInf = new DecFlowInf();
        decFlowInf.setFlowId(findCondition.getFlowId());
        decFlowInf.setFlowName(findCondition.getFlowName());
        decFlowInf.setRemark(findCondition.getRemark());
        decFlowInf.setPerId(findCondition.getPerId());

        int res = decCtctionFlowMapper.update(decFlowInf);

        int del = decCtctionPhaseMapper.deletePhaseByFlowid(findCondition.getFlowId());

        for (DecPhaseVo decPhaseBase : findCondition.getPhaselist()) {
            DecPhaseInf decPhaseInf = new DecPhaseInf();
            decPhaseInf.setPhaseId(String.valueOf(SnowFlake.getInstance().nextId()));
            decPhaseInf.setPhaseName(decPhaseBase.getPhaseName());
            decPhaseInf.setPhaseRank(decPhaseBase.getPhaseRank());
            decPhaseInf.setFlowId(findCondition.getFlowId());
            if (decPhaseBase.getPhaseRank() == 0) {
                decPhaseInf.setVaildFlag(1);
            } else {
                decPhaseInf.setVaildFlag(0);
            }
            decPhaseInf.setCtcId("temple");

            int rs = decCtctionPhaseMapper.addPhase(decPhaseInf);
        }
        return res;
    }

    @Override
    public int deleteById(String flowId) {
        int count = decCtctionFlowMapper.getDecFlowInfLength(flowId);
        if (count != 1) {
            return 0;
        }
        int result = decCtctionFlowMapper.delete(flowId);
        int sr = decCtctionPhaseMapper.deletePhaseByFlowid(flowId);
        return result;
    }

    @Override
    public Integer getDecFlowInfSize() {
        return decCtctionFlowMapper.getDecFlowInfSize();
    }


    //查询工地流程模板详情
    @Override
    public DecFlowFindCondition findById(String flowId) {
        return decCtctionFlowMapper.findFlowById(flowId);
    }

    @Override
    public List<DecPhaseInf> getRelFlowPhaselist(DecPhaseFindCondition findCondition) {

        List<DecPhaseInf> res = decCtctionPhaseMapper.phaselist(findCondition.getCtcId());
        return res;
    }

    @Override
    public JsonResult applyRelFolw(DecPhaseInf findCondition) {
        JsonResult jsonResult = new JsonResult();
        DecPhaseInf phaseInf = decCtctionPhaseMapper.findPhaseById(findCondition.getPhaseId());
        if (phaseInf.getVaildFlag() == 4){
            jsonResult.setMsg("该阶段已完成，不可再次提交");
            jsonResult.setCode(JsonResult.SUCCESS_CODE);
            return jsonResult;
        }else if(phaseInf.getVaildFlag() == 3){
            DecPhaseInf nextPhase = decCtctionPhaseMapper.findNextPhase(phaseInf);
            nextPhase.setVaildFlag(1);
            decCtctionPhaseMapper.updateVailedFlag(nextPhase);
        }
        phaseInf.setVaildFlag(phaseInf.getVaildFlag()+1);
        decCtctionPhaseMapper.updateVailedFlag(phaseInf);
        if (phaseInf.getVaildFlag() == 2 ){
            jsonResult.setMsg("已完成申请，等待业主审批");
        }else if (phaseInf.getVaildFlag() == 3 ){
            jsonResult.setMsg("已完成审批");
        }else if (phaseInf.getVaildFlag() == 4 ){
            jsonResult.setMsg("该阶段已完成");
        }
        jsonResult.setCode(JsonResult.SUCCESS_CODE);
        return jsonResult;
    }
}
